
function loadRegion(regionid, selectName, parentCode, callback) {
	var params = null;
	var targetUrl = context_path + "/topRegions.do";
	if (parentCode) {
		params = {id:parentCode};
		targetUrl = context_path + "/regions.do";
	}
	var select = $("#" + regionid + " select[name='" + selectName + "']");
	if (!select.exists()) {
		return;
	}
	if (parentCode == "00") {
		select.html("");
		select.parent().hide();
		return;
	}
	jtool.get(targetUrl, function (msg) {
		var arr = msg;
		select.html("");
		if (!arr || arr.length <= 0) {
			select.parent().hide();
			return;
		} else {
			select.parent().show();
			for (var i = 0; i < arr.length; i++) {
				$("<option></option>").val(arr[i][0]).html(arr[i][1]).appendTo(select);
			}
		}
		if (callback) {
			callback(arr);
		}
	}, params, true, "json");
}
function getRegion(regionid, selectName, parentCode) {
	$("#regionId").val(parentCode);
	loadRegion(regionid, selectName, parentCode, function (arr) {
		$("#regionId").val(arr[0][0]);
		if (selectName == "province") {
			getRegion(regionid, "city", arr[0][0]);
		} else {
			if (selectName == "city") {
				getRegion(regionid, "district", arr[0][0]);
			}
		}
	});
}
function initRegion(regionid, code, province, city, district) {
	if (!code) {
		code = "";
	}
	loadRegion(regionid, province, null, function (arr) {
		var select = $("#" + regionid + " select[name='" + province + "']");
		if (code == "" || code.length < 2) {
			code = select.val();
		}
		var provinceCode = code.substring(0, 2);
		select.val(provinceCode);
		if (city) {
			var select = $("#" + regionid + " select[name='" + city + "']");
			loadRegion(regionid, city, provinceCode, function (arr2) {
				if (code.length < 4) {
					code = select.val();
				}
				var cityCode = code.substring(0, 4);
				select.val(cityCode);
				if (district) {
					loadRegion(regionid, district, cityCode, function (arr3) {
						var select = $("#" + regionid + " select[name='" + district + "']");
						if (code.length > 4) {
							select.val(code);
						}
					});
				}
			});
		}
	});
}
function setRegion() {
	var region = $("#region select[name='city']").val();
	if (region == null || region == "") {
		region = $("#region select[name='province']").val();
		if (region == null || region == "") {
			alert("\u8bf7\u9009\u62e9\u533a\u57df!");
			return false;
		}
	}
	$("#regionId").val(region);
	$("#regionForm").submit();
	return true;
}

